using System;
using System.Collections;
using GoogleMobileAds.Api;
using UnityEngine;

public class Plug : MonoBehaviour
{
	private string adUnitId;

	private string rewardId;

	private void Awake()
	{
		Library.plug = this;
		string appId = "ca-app-pub-9466798668655932~3107976898";
		adUnitId = "ca-app-pub-9466798668655932/3654771808";
		rewardId = "ca-app-pub-9466798668655932/8822979876";
		MobileAds.Initialize(appId);
	}

	public void Interstital()
	{
		if (Application.internetReachability != 0)
		{
			InterstitialAd interstitialAd = new InterstitialAd(adUnitId);
			interstitialAd.OnAdOpening += HandleOnAdOpened;
			interstitialAd.OnAdClosed += HandleOnAdClosed;
			AdRequest request = new AdRequest.Builder().Build();
			interstitialAd.LoadAd(request);
			StartCoroutine(CheckingInterstital(interstitialAd));
		}
	}

	public void RewardVideo()
	{
		if (Application.internetReachability != 0)
		{
			RewardBasedVideoAd instance = RewardBasedVideoAd.Instance;
			instance.OnAdOpening += HandleOnAdOpened;
			instance.OnAdClosed += HandleOnAdClosed;
			AdRequest request = new AdRequest.Builder().Build();
			instance.LoadAd(request, rewardId);
			StartCoroutine(CheckingReward(instance));
		}
	}

	private IEnumerator CheckingInterstital(InterstitialAd interstitial)
	{
		yield return new WaitWhile(() => !interstitial.IsLoaded());
		interstitial.Show();
	}

	private IEnumerator CheckingReward(RewardBasedVideoAd rewardVideo)
	{
		yield return new WaitWhile(() => !rewardVideo.IsLoaded());
		rewardVideo.Show();
	}

	public void HandleOnAdOpened(object sender, EventArgs args)
	{
		AudioListener.pause = true;
	}

	public void HandleOnAdClosed(object sender, EventArgs args)
	{
		AudioListener.pause = false;
	}
}
